void serialEvent(Serial AMRport) {  
  // get the ASCII string:
  tempString = AMRport.readStringUntil('\n');
  tempString = trim(tempString);
  if (calculateChecksum(tempString) == 1) {
    // The checksum was invalid, erase tempString so we don't save the invalid data to the file
    tempString = "";
    return;
  }



  String[] separateData = splitTokens(tempString, ",");
  if (separateData[0].equals("$UMSCN")) {
    String[] newSeparateData = splitTokens(separateData[1], "*"); // Separate the long string into two which we'll only use the first element
    parseNoiseFloorData(newSeparateData[0]); // separateData[1] is * and our checksum, so we don't want to parse that data
  }
  else {
    for (int i = stringHistory - 1; i > 0; i--) {
      // bump our old strings down the array lists
      lastTenStrings[i] = lastTenStrings[i-1];
      lastTenTimes[i] = lastTenTimes[i-1];
      lastTenColors[i] = lastTenColors[i-1];
    }
    // Then save our newest data to [0] for the two arrays
    lastTenStrings[0] = trim(tempString);
    lastTenTimes[0]   = nf(hour(), 2, 0) + ":" + nf(minute(), 2, 0) + ":" + nf(second(), 2, 0);
    if (separateData.length == 6) { // Make sure we're looking at a FULL ON/UMSCP string
      if (separateData[1].equals("37287446")) { // My Meter
        lastTenColors[0] = color(255, 200, 0);
      }
      else if (parseInt(separateData[2]) == 4) { // Electric meters
        lastTenColors[0] = color(255, 0, 0);
      }
      else if (parseInt(separateData[2]) != 4) { // Other meters
        lastTenColors[0] = color(255, 0, 255);
      }
    }
    else {
      lastTenColors[0] = color(40, 40, 40); // Messages and stuff
    }

    saveString = tempString;
    //saveToLog();

    if (!separateData[0].equals("$UMMSG")) { // Ignore MSG data when seeing if we've received a new AMR device serial number
      checkSerialNumberExists(parseInt(separateData[1])); // See if the number we just received is a new AMR device serial number
      // If it is a new serial number it will put it into our arrays and stuff
      
      
      for (int i = 0; i < frequencyList.length; i++) {
        if (parseInt(separateData[4]) == frequencyList[i]) {
          String[] signalStrength = splitTokens(separateData[5], "*");
          spectrogram[i] = parseFloat(signalStrength[0]);
          hiliteFreq = i;
          break;
        }
      }
    }
  }
}

void serialSetup() {
  AMRport.write("FULL ON\n");
  delay(250);
  AMRport.write("RBKT\n");
  delay(250);
  AMRport.write("WGHT 5\n");
}

void serialPBKT() {
  AMRport.write("PBKT\n");
}

void serialReset() {
  AMRport.write("RSET\n");
  delay(250);
  AMRport.write("FULL ON\n");
}

void getNoiseFloorData() {
  AMRport.write("SSCN\n");
}

void parseNoiseFloorData(String data) {
  String[] tokens = splitTokens(data, ":"); // The spectrogram data is colon separated
  for (int i = 0; i < 60; i++) {
    noiseFloor[i] = parseFloat(tokens[i]);
  }
}

